package com.vogue.supplymall.account.config;

//import java.util.ArrayList;

import com.vogue.supplymall.account.shiro.filter.LoginAuthenticationFilter;
import com.vogue.supplymall.account.shiro.filter.MallAuthenticationFilter;
import com.vogue.supplymall.account.shiro.filter.WholeAuthenticationFilter;
import com.vogue.supplymall.common.config.JacksonConvert;
import com.vogue.supplymall.common.domain.tool.MessageSourceUtils;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.List;

//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.http.HttpHeaders;
//import org.springframework.http.HttpMethod;
//import org.springframework.web.bind.annotation.CrossOrigin;
//import org.springframework.web.servlet.config.annotation.CorsRegistry;
//import com.vogue.ssm.controller.help.ErrorMsg;


/**
 * Created by Administrator on 2017-11-27.
 */
@Configuration
@ComponentScan("com.vogue.supplymall.common.domain.tool")
@ComponentScan("com.vogue.supplymall.shop.common")
@ComponentScan("com.vogue.supplymall.shop.domain")
@ComponentScan("com.vogue.supplymall.match.domain")
@ComponentScan("com.vogue.supplymall.order.domain")
@ComponentScan("com.vogue.supplymall.upload.domain")
@ComponentScan("com.vogue.supplymall.order.common")
@ComponentScan("com.vogue.supplymall.admin.business.domain.dao")
@ComponentScan("com.vogue.supplymall.admin.business.domain.service")
@ComponentScan("com.vogue.supplymall.payment.api")
@MapperScan({"com.vogue.supplymall.account.domain.mapper",
        "com.vogue.supplymall.shop.domain.mapper",
        "com.vogue.supplymall.match.domain.mapper",
        "com.vogue.supplymall.order.domain.mapper",
        "com.vogue.supplymall.admin.business.domain.mapper",
        "com.vogue.supplymall.cross.domain.mapper"})
@ImportResource(locations = {"classpath:context.xml", "classpath:base-context.xml"})
public class AppConfig extends WebMvcConfigurerAdapter {

//	private static final Logger logger = LoggerFactory.getLogger(AppConfig.class);

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        JacksonConvert.convert(converters);
    }
	
    /*@SuppressWarnings("deprecation")
	@Override
    public void addCorsMappings(CorsRegistry registry) {
    	logger.debug("==== addCorsMappings ====");
//        registry.addMapping("/**").allowedOrigins("http://192.168.7.25:8080")
//                .allowedMethods("GET", "HEAD", "POST","PUT", "DELETE", "OPTIONS")
//                .allowCredentials(true).maxAge(3600);
        
        List<String> allowedOrigins = new ArrayList<>();		
		allowedOrigins.add("http://192.168.11.104:8088");
		allowedOrigins.add("http://192.168.7.108:8080");
		String[] objects = allowedOrigins.toArray(new String[allowedOrigins.size()]);

		
				// 允许向该服务器提交请求的URI，*表示全部允许
		registry.addMapping("/**").allowedOrigins(objects)
				// 允许提交请求的方法，*表示全部允许
				.allowedMethods(HttpMethod.DELETE.name(), HttpMethod.GET.name(), HttpMethod.HEAD.name(),
						HttpMethod.POST.name(), HttpMethod.PUT.name(), HttpMethod.OPTIONS.name())
				// 允许的头信息,*标识全部允许
				.allowedHeaders(CrossOrigin.DEFAULT_ALLOWED_HEADERS)
				// 暴露的头信息
				.exposedHeaders(HttpHeaders.SET_COOKIE, "pname", "content-type")
				// 允许Cookie跨域，在做登录校验的时候有用
				.allowCredentials(CrossOrigin.DEFAULT_ALLOW_CREDENTIALS)
				// 预检请求的缓存时间（秒），即在这个时间段里，对于相同的跨域请求不会再预检了
				.maxAge(CrossOrigin.DEFAULT_MAX_AGE);
//        ErrorMsg.deftemp="<h1>%s</h1>";
    }*/

    @Bean
    public FilterRegistrationBean regAuth(MallAuthenticationFilter filter) {
        FilterRegistrationBean registration = new FilterRegistrationBean(filter);
        registration.setEnabled(false);
        return registration;
    }

    @Bean
    public FilterRegistrationBean regLogin(LoginAuthenticationFilter filter) {
        FilterRegistrationBean registration = new FilterRegistrationBean(filter);
        registration.setEnabled(false);
        return registration;
    }

    @Bean
    public FilterRegistrationBean regWhole(WholeAuthenticationFilter filter) {
        FilterRegistrationBean registration = new FilterRegistrationBean(filter);
        registration.setEnabled(false);
        return registration;
    }

    @Bean(name = "messageSourceUtils")
    public MessageSourceUtils messageSourceUtils() {
        return new MessageSourceUtils("i18n/accountmessages");
    }
}
